﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.BLL;
using _4T.Data;
using _4T.Data.IDataContext;
using CMS.Core.Libs;


namespace _4T.ASCX
{
    public partial class ProfilePaymentControl : System.Web.UI.UserControl
    {

        #region Field

        private List<string> imageArrayPath = new List<string>();
        private List<string> videoArrayPath = new List<string>();
        CMSConsiderAndDecide cmsConsiderAndDecide;
        string ApproveProfileID;

        #endregion

        #region Contructor
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["imageArrayPath"] = imageArrayPath;
                Session["videoArrayPath"] = videoArrayPath;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["pid"]))
            {
                this.LoadFilePatient(Request.QueryString["pid"]);
            }

        }
        #endregion

        #region Private

        private void LoadFilePatient(string ApproveProfileID)
        {
            CMSApproveProfile cmsApproveProfile = new DAL.ApproveProfileDAL().GetProfileApproveByApproveProfileID(ApproveProfileID);
            cmsConsiderAndDecide = new DAL.ConsiderAndDecideDAL().GetConsiderAndDecideByID(cmsApproveProfile.ConsiderAndDecideID);
            txtDiseaseName.Text = cmsConsiderAndDecide.DiseaseName;
            txtPhaseOfDisease.Text = cmsConsiderAndDecide.PhaseOfDisease;
            if (cmsConsiderAndDecide.IsTreat)
                rdoIsTreatYes.Checked = true;
            else
                rdoIsTreatNo.Checked = true;

            if (cmsConsiderAndDecide.IsRecover)
                rdoIsRecoverYes.Checked = true;
            else
                rdoIsRecoverNo.Checked = true;


            txtAnticipateSpend.Text = cmsConsiderAndDecide.AnticipateSpend;
            txtTreatTime.Text = cmsConsiderAndDecide.TreatTime;
            txtCommentOfDoctor.Text = cmsConsiderAndDecide.CommentOfDoctor;

            if (cmsConsiderAndDecide.IsPoor)
                rdoIsPoorYes.Checked = true;
            else
                rdoIsPoorNo.Checked = true;

            if (cmsConsiderAndDecide.IsDirectLabour)
                rdoIsDirectLabourYes.Checked = true;
            else
                rdoIsDirectLabourNo.Checked = true;

            txtCauseIll.Text = cmsConsiderAndDecide.CauseIll;
            //
            //
            //
            string profileInfoCode = cmsConsiderAndDecide.ProfileName;
            // Load patient info
            PatientInfoBLL patientBLL = new PatientInfoBLL();
            _4T.Data.PatientInfo patient = patientBLL.GetPatientInfo(profileInfoCode);
            if (patient != null)
            {
                lbPatientName.Text = patient.PatientName;
                lbDOB.Text = patient.Birthday.ToShortDateString();
                ddlGender.SelectedValue = patient.Gender;
                lbIDNumber.Text = patient.IDNumber;
                lbPermAddr.Text = patient.PermanentAddress;
                lbTempAddr.Text = patient.TemporaryAddress;
                lblMobileNumber.Text = patient.MobileNumber;
                lbPhoneNumber.Text = patient.PhoneNumber;
                lbRepresentativeName.Text = patient.EmergencyRepresentative;
                lbRepresentativePhone.Text = patient.RepresentativePhoneNumber;
                lbLocalContactName.Text = patient.LocalContactPoint;
                lbLocalContactPhone.Text = patient.LocalContactPointPhoneNumber;
                lbMedicalStaff.Text = patient.LocalMedicalStaff;
                lbMedicalStaffPhone.Text = patient.LocalMedicalStaffPhoneNumber;
                lbMedicalFacility.Text = patient.MedicalFacility;
                lbNote.Text = patient.Note;
            }

            // Load medical info
            MedicalInfoBLL medicalBLL = new MedicalInfoBLL();
            _4T.Data.MedicalInfo medical = medicalBLL.GetMedicalInfo(profileInfoCode);
            if (medical != null)
            {
                lblDiagnosis.Text = medical.Diagnosis;
                lbMedicalTime.Text = medical.MedicalTime.ToShortDateString();
                lbTreatmentMethod.Text = medical.TreatmentMethod;
                lbSituation.Text = medical.Situation;
                lbSupport.Text = medical.Support;
            }

            // Load info supplier info
            InfoSupplierInfoBLL infoSupplierBLL = new InfoSupplierInfoBLL();
            _4T.Data.InfoSupplierInfo supplier = infoSupplierBLL.GetInfoSupplierInfo(profileInfoCode);
            if (supplier != null)
            {
                lbInfoSupplierName.Text = supplier.SupplierName;
                lbInfoSupplierPhone.Text = supplier.PhoneNumber;
                lbEmail.Text = supplier.Email;
            }
            /*if (Request.QueryString["pid"] != null)
            {
                result = new ConsiderAndDecideBLL().GetConsiderAndDecideByID(Guid.Parse(Request.QueryString["pid"]));
                lblProfileCode.Text = result.ProfileName;

                var patientObj = new PatientInfoBLL().GetPatientInfo(result.ProfileName);
                if (patientObj != null)
                {
                    lblProfileName.Text = !string.IsNullOrEmpty(patientObj.PatientName) ? patientObj.PatientName : string.Empty;
                    lblDateOfBirth.Text = patientObj.Birthday.ToString();
                    lblCMND.Text = patientObj.IDNumber;
                    lblGender.Text = patientObj.Gender;
                    lblPhone.Text = patientObj.MobileNumber;
                    lblAdress1.Text = patientObj.PermanentAddress;
                    lblAdress2.Text = patientObj.TemporaryAddress;

                }

                lblFilterName.Text = result.ConsiderAndDecideName;
                lblFiltDate.Text = result.CreatedDate.ToString();
                lblCommentOfFilter.Text = result.CommentOfConsider;
            }
            */
        }


        private string GetProfileInfoCodeFromProfileCode(string profileCode)
        {
            return new ProfileInfoBLL().GetProfileInfoCodeFromProfileCode(profileCode);
        }

        private PersonalInfo GetPersonalInfo(string personalCode)
        {
            return new PersonalInfoBLL().GetPersonalInfo(personalCode);
        }
        #endregion


        #region ICMSConsiderAndDecide
        private string _imageUrl;
        public string ImageUrl
        {
            get
            {
                imageArrayPath = (Session["imageArrayPath"] as List<string>);
                if (imageArrayPath != null && imageArrayPath.Count > 0)
                {
                    foreach (var item in imageArrayPath)
                    {
                        _imageUrl += item;
                        break;
                    }
                    return _imageUrl;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _imageUrl = value; }
        }

        private string _videoUrl;
        public string VideoUrl
        {
            get
            {
                videoArrayPath = (Session["videoArrayPath"] as List<string>);
                if (videoArrayPath != null && videoArrayPath.Count != 0)
                {
                    foreach (var item in videoArrayPath)
                    {
                        _videoUrl += item;
                        break;
                    }
                    return _videoUrl;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _videoUrl = value; }
        }
        #endregion

        #region Event

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string sessionVideoUrl = VideoUrl;
            string sessionImageUrl = ImageUrl;
            Session["imageArrayPath"] = null;
            Session["videoArrayPath"] = null;
            CMSApproveProfile approveProfile = new ApproveProfileBLL().GetProfileApproveByApproveProfileID(Request.QueryString["pid"]);

            int updateStage = (int)ProfileStage.Payment + 1;
            ProfileStatus profileStatus = new ProfileStatus
            {
                PaymentPerson = Session["User"] != null ? Session["User"].ToString() : string.Empty,
                ProfileCode = approveProfile.ProfileID,
                Stage = updateStage
            };

            DateTime createdDate = new DateTime();

            try
            {
                if (!string.IsNullOrEmpty(txtCreatedDate.Text))
                    createdDate = Convert.ToDateTime(txtCreatedDate.Text);
            }
            catch
            {
                createdDate = DateTime.Now;
            }

            if (new ProfileStatusBLL().UpdateProfileStatus(profileStatus) && new DAL.PaymentProfileDAL().appStageLinkPaymentProfileDAL(approveProfile.ProfileID, sessionVideoUrl, sessionImageUrl, txtComment.Text, createdDate) > 0)
            {
                HttpContext.Current.Response.Redirect("~/ProfileApproveList.aspx");
            }
        }

        #endregion


    }
}